package com.mdnote.practice.array;

/**
 * @author Rhythm-2019
 * @version 1.0
 * @date 2020/11/18 9:55
 * @Email rhythm_2019@163.com
 * @Description 134  加油站
 */
public class LeetCode134 {

    public int canCompleteCircuit(int[] gas, int[] cost) {
        int i = 0, n = gas.length;
        while (i < n) {
            int cut = 0;
            int sumOfGas = 0, sumOfCost = 0;
            while (cut < n) {
                int j = (i + cut) % n;
                sumOfGas += gas[j];
                sumOfCost += cost[j];
                if (sumOfCost > sumOfGas) {
                    break;
                }
                cut++;
            }
            if (cut == n) {
                return i;
            } else {
                i = i + cut + 1;
            }

        }
        return -1;
    }
}
